/*
 *  Copyright 2019-2023 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.modules.information.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BizException;
import me.zhengjie.modules.information.domain.Doc;
import me.zhengjie.modules.information.service.DocService;
import me.zhengjie.modules.information.domain.vo.DocQueryCriteria;
import lombok.RequiredArgsConstructor;
import java.util.List;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import me.zhengjie.utils.PageResult;

/**
 * @author yiwei.he
 * @date 2024-05-17
 **/
@RestController
@RequiredArgsConstructor
@Api(tags = "文案库管理")
@RequestMapping("/api/doc")
public class DocController {

    private final DocService docService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
//    @PreAuthorize("@el.check('doc:list')")
    public void exportDoc(HttpServletResponse response, DocQueryCriteria criteria) throws IOException {
        docService.download(docService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询文案库")
    @ApiOperation("查询文案库")
//    @PreAuthorize("@el.check('doc:list')")
    public ResponseEntity<PageResult<Doc>> queryDoc(DocQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(docService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增文案库")
    @ApiOperation("新增文案库")
//    @PreAuthorize("@el.check('doc:add')")
    public ResponseEntity<Object> createDoc(@Validated @RequestBody Doc resources) throws BizException {
        docService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改文案库")
    @ApiOperation("修改文案库")
//    @PreAuthorize("@el.check('doc:edit')")
    public ResponseEntity<Object> updateDoc(@Validated @RequestBody Doc resources){
        docService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除文案库")
    @ApiOperation("删除文案库")
//    @PreAuthorize("@el.check('doc:del')")
    public ResponseEntity<Object> deleteDoc(@RequestBody List<Long> ids) {
        docService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("/{docCode}")
    @Log("通过code查询文案")
    @ApiOperation("通过code查询文案")
    public ResponseEntity<Doc> qryByCode(@PathVariable @NotNull String docCode) throws BizException {
        return new ResponseEntity<>(docService.qryByCode(docCode),HttpStatus.OK);
    }

}